#!/usr/bin/env python2
#
# Component that watches for a system clock jump and reconnects
# NetworkManager if a significant jump has occured.
#
# Name: Clock Watcher 
# Author: Douglas Turner
# Revision: 12 January 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License
#
#
#
import grp
import os
import pwd
import pynotify
import resource
import subprocess
import sys
import time

class TimeWatcher():
	def __init__(self, sensitivity):
		self.last_clock = 0
		self.firstRunFlag = 0
		self.sensitivity = sensitivity + 45
		self.Notify()
	#	
	def Notify(self):
		tempTime = time.time()

		if self.firstRunFlag != 0:
			if abs(tempTime - self.last_clock) > self.sensitivity:
				try:
					subprocess.call(["nmcli", "nm", "enable", "false"])
					subprocess.call(["nmcli", "nm", "enable", "true"])
				except Exception:
					pass
				time.sleep(30)
			#
		#
		
		self.last_clock = tempTime
		self.firstRunFlag = 1
		self.start_checking()
	#
	def start_checking(self):
		time.sleep(30)
		self.Notify()
	#
#


if __name__ == '__main__':
	if len(sys.argv) > 2:
		print "Initial User Failure"
		print >> sys.stderr, "Usage: ", sys.argv[0], "[sensitivity]"
		sys.exit(2)
	#
	
	sensitivity = 0
	if len(sys.argv) == 2:
	   sensitivity = int(sys.argv[1])
	#

	TimeWatcher(sensitivity)
#
